#include <Windows.h>
#include <tchar.h>
#include <assert.h>
//extern BOOL SetLayeredWindowAttributes(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT IDC_PRBALPHA		= 1001;
	static const UINT IDC_STCALPHAVALUE	= 1002;
	
	switch (nMsg)
	{
		case WM_CREATE:
		{
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
			UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);
			
			CreateWindow(_T("STATIC"), _T("Alpha Value: 255"), WS_CHILD|WS_VISIBLE|SS_CENTER, 0, 0, 800, 20, hWnd, (HMENU)IDC_STCALPHAVALUE, NULL, NULL);
			HWND hScroll = CreateWindow(_T("ScrollBar"), _T(""), WS_CHILD|WS_VISIBLE, 0, 20, 790, 30, hWnd, (HMENU)IDC_PRBALPHA, NULL, NULL);
			SCROLLINFO lpsi	 = {0};
			lpsi.cbSize	= sizeof(SCROLLINFO);
			lpsi.fMask	= SIF_ALL;
			lpsi.nMin	= 0;
			lpsi.nMax	= 255;
			lpsi.nPage	= 1;
			lpsi.nPos	= 255;
			lpsi.nTrackPos = 1;
			SetScrollInfo(GetDlgItem(hWnd, IDC_PRBALPHA), SB_CTL, &lpsi, FALSE);
			SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(hWnd, GWL_EXSTYLE));
			SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), lpsi.nPos, LWA_ALPHA);

			break;
		}
		case WM_HSCROLL:
		{
			int nPos = GetScrollPos(GetDlgItem(hWnd, IDC_PRBALPHA), SB_CTL);
			switch (LOWORD(wParam))
			{  
				case SB_ENDSCROLL:
					break;
				case SB_LINELEFT:
					if (nPos > 0) nPos--;
					break;
				case SB_LINERIGHT:
					if (nPos < 255) nPos++;
					break;
				case SB_PAGELEFT:
					if (nPos - 5 <= 0)
						nPos = 0;
					else 
						nPos -= 5;
					break;
				case SB_PAGERIGHT:
					if (nPos + 5 >= 255) 
						nPos = 255;
					else 
						nPos += 5;
					break;
				case SB_THUMBTRACK:
				case SB_THUMBPOSITION:
					nPos = HIWORD(wParam);
					break;
				case SB_LEFT:
					nPos = 10;
					break;
				case SB_RIGHT:
					nPos = 255;
					break;			
			}
			SetScrollPos(GetDlgItem(hWnd, IDC_PRBALPHA), SB_CTL, nPos, TRUE);
		
			TCHAR szValue[MAX_PATH];
			_stprintf(szValue, _T("Alpha value: %d"), nPos);
			SetWindowText(GetDlgItem(hWnd, IDC_STCALPHAVALUE), szValue);
	
			SetLayeredWindowAttributes(hWnd, RGB(255, 0, 0), nPos, LWA_ALPHA|LWA_COLORKEY);
			
			break;
		}
		case WM_PAINT:
		{
			RECT rtRect = {0, 120, 800, 350};
			LOGBRUSH logBrush	= {0};
			logBrush.lbColor	= RGB(255, 0, 0);
			logBrush.lbStyle	= 0;
			logBrush.lbHatch	= 0;
			HBRUSH hBrush = CreateBrushIndirect(&logBrush);

			PAINTSTRUCT pt;
			HDC hDC = BeginPaint(hWnd, &pt);
			FillRect(hDC, &rtRect, hBrush);
			EndPaint(hWnd, &pt);

			DeleteObject(hBrush);
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szClsName, szWndCaption, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 
								0, 0, 800, 480, NULL, NULL, hInstance, hInstance);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo0036");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}